﻿using System;
using System.Collections.Generic;
using LuckyLoot.Domain.Aggregate;
using LuckyLoot.Domain.Entities;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.Common.Repositories;
using LuckyLoot.Common.Contexts;
using System.Linq;

namespace LuckyLoot.ServiceFacade.Implementation
{
    public class ReminderService : IReminderService
    {
        #region IReminderService Members

        public IEnumerable<Supply> LowStockSupplies()
        {
            using (var supplyRepository = new Repository<Supply, ApplicationContext>())
            {
                var lowStock = supplyRepository.GetByCriteria(supply => supply.Amount < 5).ToList<Supply>();
                return lowStock;                
            }
        }

        public IEnumerable<Supply> ExpiringSupplies()
        {
            using (var supplyRepository = new Repository<Supply, ApplicationContext>())
            {
                var topDate = DateTime.Today.AddDays(10);

                var expiring = supplyRepository.GetByCriteria(supply => supply.Expiration.Value <= topDate).ToList<Supply>();
                return expiring;
            }
        }

        public IEnumerable<Tool> ExpiringSterilizationTools()
        {
            using (var toolRepository = new Repository<Tool, ApplicationContext>())
            {
                var sterilizations = toolRepository.GetAll().ToList<Tool>();

                var expiringSterilization = new List<Tool>();
                foreach(var tool in sterilizations)
                {
                    if (tool.LastSterilization.HasValue)
                    {
                        if (DateTime.Today >= tool.LastSterilization.Value.Date.AddDays(25))
                            expiringSterilization.Add(tool);
                    }
                }


                return expiringSterilization;
            }
        }

        public IEnumerable<Apointment> WeekAppointments()
        {
            using (var appointmentRepository = new Repository<Apointment, ApplicationContext>())
            {
                var startDate = DateTime.Today;
                var endDate = DateTime.Today.AddDays(7);

                var weekAppointments = appointmentRepository.GetByCriteria(appointment => appointment.Date >= startDate && appointment.Date <= endDate).ToList<Apointment>();
                return weekAppointments;
            }
        }

        #endregion
    }
}
